package xian.woniuxy.zm_treeset;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

class Fruit implements Comparable<Fruit> {
    private String name;
    private int weight;
    private int price;

    public Fruit() {
    }

    public Fruit(String name, int weight, int price) {
        this.name = name;
        this.weight = weight;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                ", weight=" + weight +
                ", price=" + price +
                '}';
    }

    /*
        返回值小于0， 说明新来比旧元素小
        返回值大于0， 说明新来比旧元素大
        返回值等于0， 说明新来和旧元素相等
     */
    @Override
    public int compareTo(Fruit that) {
        System.out.println(this + " vs " + that);
        int r = -Integer.compare(this.getWeight(), that.getWeight());
        if (r == 0) {
            return Integer.compare(this.price, that.price);
        }
        return r;
    }
}

// =========================================================================

public class App {

    public static void main(String[] args) {
        Fruit f = new Fruit("苹果", 5, 10);
        Fruit f2 = new Fruit("西瓜", 5, 8);
        Fruit f3 = new Fruit("桃子", 5, 1);
        Fruit f4 = new Fruit("葡萄", 4, 3);
        Fruit f5 = new Fruit("香蕉", 4, 7);
        Fruit f6 = new Fruit("芭拉", 4, 6);

        Set set = new TreeSet();
        set.add(f);
        set.add(f2);
        set.add(f3);
        set.add(f4);
        set.add(f5);
        set.add(f6);

        set.stream().forEach(System.out::println);
    }
}
